﻿using MySql.Data.MySqlClient;
using RantopadService.Conn;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace RantopadService.Business
{
    public class UserClientVBusiness
    {
        public static DataTable getAllUserClientVs(string wherelike)
        {
            string mysql = "SELECT  a.*, b.NAME as CLIENT_NAME from user_client_v a,client_type b where (a.USER_ID like '%{0}%' )  and a.CLIENT_TYPE=b.ID ";
            mysql = string.Format(mysql, wherelike);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);

            myconnection.Close();
            return dt;
        }
        public static DataTable getAllUserClientVs(string wherelike, string client_typeId)
        {
            string mysql = "SELECT  a.*, b.NAME as CLIENT_NAME from user_client_v a,client_type b where (a.USER_ID like '%{0}%' ) and a.CLIENT_TYPE = '{1}' and a.CLIENT_TYPE=b.ID ";
            //string mysql = "SELECT * from user_client_v  where (USER_ID like '%{0}%' ) and CLIENT_TYPE like '%{1}%'";
            if (client_typeId == "0")
                return getAllUserClientVs(wherelike);
            mysql = string.Format(mysql, wherelike, client_typeId);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);

            myconnection.Close();
            return dt;
        }
        public static int getAllUserClientVsCount()
        {
            string mysql = "SELECT count(*) as count from user_client_v ";
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
            { myconnection.Close(); return 0; }
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }
        public static int getAllUserClientVsCount(string wherelike, string client_typeId)
        {
            string mysql = "SELECT count(*) as count from user_client_v where (USER_ID like '%{0}%' )  and CLIENT_TYPE like '%{1}%'";
            mysql = string.Format(mysql, wherelike, client_typeId);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
            { myconnection.Close(); return 0; }
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }
        public static int checkUserClientVId(string user_client_vID)
        {
            var count = checkData("USER_ID", user_client_vID);
            return count;
        }
        public static int checkData(string colNM, string user_client_vID)
        {
            string mysql = "SELECT count(*) as count from user_client_v where {0} = '{1}' ";
            mysql = string.Format(mysql, colNM, user_client_vID);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);
            MySqlDataReader dr = mycommand.ExecuteReader();

            if (!dr.Read())
            { myconnection.Close(); return 0; }
            var countstr = dr["count"].ToString();
            var count = Convert.ToInt32(countstr);
            myconnection.Close();
            return count;
        }

        public static int checkUserClientVEmail(string user_client_vEmail)
        {
            var count = checkData("EMAIL", user_client_vEmail);
            return count;
        }
        public static int delUserClientV(string userID, string client_typeID)
        {
            string mysql = "delete from user_client_v where USER_ID='{0}' and CLIENT_TYPE='{1}'";
            mysql = string.Format(mysql, userID, client_typeID);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

            var count = mycommand.ExecuteNonQuery();

            myconnection.Close();
            return count;
        }


        public static string getUserClientV(string userID, string client_typeID)
        {
            string mysql = "SELECT  a.*, b.NAME as CLIENT_NAME from user_client_v a,client_type b where (a.USER_ID = '{0}' ) and a.CLIENT_TYPE = '{1}' and a.CLIENT_TYPE=b.ID ";
            mysql = string.Format(mysql, userID, client_typeID);
            //创建数据库连接
            var myconnection = MyMySqlHelper.GetConn();
            myconnection.Open();
            var dataAdapter = new MySqlDataAdapter(mysql, myconnection);
            var dt = new DataTable();
            dataAdapter.Fill(dt);

            myconnection.Close();
            return dt.Rows[0]["CLIENT_V"].ToString();
        }

        public static int addUserClientV(string userID, string client_typeID, string clientV)
        {
            //INSERT INTO `rantopad`.`user_client_v` (`USER_ID`, `USER_PSW`, `EMAIL`, `CLIENT_TYPE`) VALUES ('aaaaa', 'a', 'liang@163.com', '0');
            int count = 0;//创建数据库连接
                var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
            try
            {
                string mysql = "insert into `user_client_v` (`USER_ID`, `CLIENT_TYPE`, `CLIENT_V`) VALUES ('{0}', '{1}', '{2}') ";
                mysql = string.Format(mysql, userID, client_typeID, clientV);
                
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception e)
            {
                myconnection.Close();
                NetLog.WriteTextLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), e.Message.ToString(), DateTime.Now);
                count = 0;
            }


            return count;
        }

        internal static int updateUserClientV(string userID, string client_typeID, string clientV)
        {
            int count = 0;//创建数据库连接
                var myconnection = MyMySqlHelper.GetConn();
                myconnection.Open();
            try
            {
                string mysql = "update `user_client_v` set `CLIENT_V`='{2}'  where `USER_ID`='{0}' and `CLIENT_TYPE`='{1}'";
                mysql = string.Format(mysql, userID, client_typeID, clientV);
                
                MySqlCommand mycommand = new MySqlCommand(mysql, myconnection);

                count = mycommand.ExecuteNonQuery();
                myconnection.Close();
            }
            catch (Exception e)
            {
                myconnection.Close();
                NetLog.WriteTextLog(System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), e.Message.ToString(), DateTime.Now);
                count = 0;
            }


            return count;
        }
    }
}